/*
 *
 * (c) Copyright Ascensio System Limited 2010-2018
 *
 * This program is freeware. You can redistribute it and/or modify it under the terms of the GNU 
 * General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html). 
 * In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that 
 * Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
 *
 * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
 * FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
 *
 * You can contact Ascensio System SIA by email at sales@onlyoffice.com
 *
 * The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display 
 * Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
 *
 * Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains 
 * relevant author attributions when distributing the software. If the display of the logo in its graphic 
 * form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE" 
 * in every copy of the program you distribute. 
 * Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
 *
*/


if (typeof ASC === "undefined")
    ASC = {};

ASC.SocketIO = function (hub) {
    var master = ASC.Resources.Master;
    if (master.Hub && master.Hub.Url) {
        var masterHub = master.Hub;
        var opts = {
            path: masterHub.Url,
            reconnectionAttempts: 5,
            perMessageDeflate: "false"
        }
        if (master.numberId && masterHub.VoipEnabled) {
            opts.query = { numberId: master.numberId };
        }
        this.socket = io(hub, opts);
    }
};

ASC.SocketIO.prototype.emit = function() {
    if (this.socket) {
        this.socket.emit.apply(this.socket, arguments);
    }
    return this;
}

ASC.SocketIO.prototype.on = function () {
    if (this.socket) {
        if (arguments[0] === "connect" && typeof arguments[1] === "function" && this.socket.connected) {
            arguments[1]();
        } else {
            this.socket.on.apply(this.socket, arguments);
        }
    }
    return this;
}

ASC.SocketIO.prototype.connect = function () {
    return this.on.call(this, "connect", arguments[0]);
}

ASC.SocketIO.prototype.reconnect_failed = function () {
    return this.on.call(this, "reconnect_failed", arguments[0]);
}

ASC.SocketIO.prototype.connected = function () {
    return this.socket && this.socket.connected;
}

ASC.SocketIO.disabled = function () {
    return !ASC.Resources.Master.Hub || !ASC.Resources.Master.Hub.Url;
}

ASC.SocketIO.init = function () {
    var master = ASC.Resources.Master;
    if (master.Hub && master.Hub.Url) {
        var url = ASC.Resources.Master.Hub.Url;
        var lastIndexSlash = url.length - 1;
        if (url.lastIndexOf("/") === url.length - 1) {
            url = url.substring(0, lastIndexSlash);
        }
        jq.ajax({
            type: "OPTIONS",
            url: url + "/?EIO=3&transport=polling",
            async: true
        });
    }
}

ASC.SocketIO.Factory = (function () {
    var chat, voip, counters, files;

    return {
        get chat() {
            if (!chat) {
                chat = new ASC.SocketIO("/chat");
            }
            return chat;
        },
        get voip() {
            if (!voip) {
                voip = new ASC.SocketIO("/voip");
            }
            return voip;
        },
        get counters() {
            if (!counters) {
                counters = new ASC.SocketIO("/counters");
            }
            return counters;
        },
        get files() {
            if (!files) {
                files = new ASC.SocketIO("/files");
            }
            return files;
        }
    };
})();

jq(document).ready(function () {
    ASC.SocketIO.init();
    ASC.SocketIO.Factory.counters
    .on('getNewMessagesCount', function (counts) {
        if (ASC.Controls.MailReader) {
            ASC.Controls.MailReader.setUnreadMailMessagesCount(counts.ma);
        }
    })
    .on('sendMailNotification', function (state) {
        ASC.Mail.Utility._showSignalRMailNotification(state);
    })
    .on('updateFolders', function (counts, shouldUpdateMailBox) {
        if (window.serviceManager) {
            window.serviceManager.updateFolders();
            if (shouldUpdateMailBox) {
                window.serviceManager.getAccounts();
            }
        } else if (ASC.Controls.MailReader) {
            ASC.Controls.MailReader.setUnreadMailMessagesCount(counts);
        }
    });
});